package com.itheima.health.controller.user;


import com.itheima.health.common.MessageConst;
import com.itheima.health.dto.MemberParam;
import com.itheima.health.dto.OrderParam;
import com.itheima.health.dto.OrderSubmitParam;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.result.Result;
import com.itheima.health.service.UserOperateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/mobile")
@Slf4j
public class UserOperateController {
    @Autowired
    private UserOperateService userOperateService;

    /**
     * 获取验证码
     */
    @PostMapping("/validateCode/send")
    public Result validateCode(MemberParam memberParam){
        log.info("[登录-用户端获取验证码]:{}", memberParam);
        userOperateService.send(memberParam);
        return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);
    }
    /**
     * C端
     * 用户登录
     */
    @PostMapping("/login/smsLogin")
    public Result smsLogin(@RequestBody MemberParam memberParam){
        log.info("[登录-用户登录]：{}", memberParam);
        userOperateService.smsLogin(memberParam);
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }

    @GetMapping("/setmeal/getSetmeal")
    public Result getSetmeal(){
        log.info("[页面项目展示-用户]：{}");
        List<Setmeal> setmealList = userOperateService.getSetmeal();
        return new Result(true, MessageConst.QUERY_SETMEALLIST_SUCCESS, setmealList);
    }
    /**
     * 根据套餐id查询套餐详情
     */
    @GetMapping("/setmeal/findById")
    public Result findSetmealById(Integer id){
        log.info("[查询套餐数据成功]: {}", id);
        Setmeal setmeal = userOperateService.getCheckGroupBySetmealId(id);
        return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, setmeal);
    }

    @PostMapping("/order/submit")
    public Result submit(@RequestBody OrderSubmitParam orderSubmitParam){
        log.info("[提交预约成功]: {}");
        //查询验证码
        MemberParam memberParam = userOperateService.queryCode(orderSubmitParam.getTelephone());
        //判断验证码是否正确
        if (memberParam.getValidateCode().equals(orderSubmitParam.getValidateCode())){
            //正确就添加预约
            userOperateService.submit(orderSubmitParam);
        }else {
            throw new BusinessRuntimeException(MessageConst.VALIDATECODE_ERROR);
        }

        return new Result(true,MessageConst.ORDER_SUCCESS);
    }
    @GetMapping("/order/findById")
    public Result findOrderById(Integer id){
        log.info("[根据id查询预约信息]: {}");
        OrderParam order = userOperateService.findOrderById(id);
        return new Result(true, MessageConst.QUERY_ORDER_SUCCESS, order);
    }
}
